﻿using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using System;
using System.ComponentModel;
using System.Linq;
using 干涉检查Demo;

namespace SqlData
{

    [Persistent(nameof(FileName))]
    [OptimisticLocking(true), Browsable(true)]
    public class FileName : XPBaseObject
    {
        public FileName(Session session) : base(session) { }

        #region  Document框架结构
    
         
        int fFileID;
        [Key(true)]
        public int FileID
        {
            get { return fFileID; }
            set { SetPropertyValue<int>(nameof(FileID), ref fFileID, value); }
        }

 
        private string fFullName;
        [Size(100)]
        public string FullName
        {
            get { return fFullName; }
            set { SetPropertyValue<string>(nameof(FullName), ref fFullName, value); }
        }

        private bool _isSelected;
        [NonPersistent]
        public bool IsSelected
        {
            get => _isSelected;
            set => SetPropertyValue(nameof(IsSelected), ref _isSelected, value);
        }

        [Association(nameof(FileName) + "-" + nameof(AbsoluteCalculation))]
        public XPCollection<AbsoluteCalculation> AbsoluteCalculation
        {
            get { return GetCollection<AbsoluteCalculation>(nameof(AbsoluteCalculation)); }
        } 

        [Association(nameof(FileName) + "-" + nameof(SingleNotCalculated))]
        public XPCollection<SingleNotCalculated> SingleNotCalculated
        {
            get { return GetCollection<SingleNotCalculated>(nameof(SingleNotCalculated)); }
        }

        #endregion


        /// <summary>
        /// 检查文件名称存在性
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool GetFileName(string fileName)
        {
            try
            {
                var name = XpoCon.UnitOfShort.Query<FileName>().FirstOrDefault(u => u.FullName == fileName);
                if (name == null)
                {
                    name = new FileName(XpoCon.UnitOfShort)
                    {
                        FullName = fileName
                    }; 
                }
                XpoCon.UnitOfShort.Save(name);
                XpoCon.UnitOfShort.CommitChanges();
                return false;
            }
            catch (Exception ex)
            {
                XpoCon.UnitOfShort.RollbackTransaction();
                XpoCon.ReleaseShortUnitOfWork();
                Msg.ErrorMessageShow(ex.Message);
                return false;
            }
            finally
            {
                XpoCon.ReleaseShortUnitOfWork();
            }
        }


    }
}

